﻿#include "editor/drawing/toolbox.h"
#include "editor/drawing/tool.h"
#include "editor/settings.h"
#include "editor/inspector.h"

using namespace anip;
using namespace anip::editor;

const vec2 ToolButtonSize = { 40, 40 };

DrawingToolbox::DrawingToolbox(Container* parent)
	: EditorWindow(parent, vec2{ 80, 500 }, L"工具")
{
	assert(Globals::drawingToolbox == nullptr); // or auto-delete?
	Globals::drawingToolbox = this;

	for (auto i : Globals::drawingTools)
	{
		auto b = ui::FlowHelper::button(*this, L"");
		_buttons.push_back(b);
		b->layoutOptions.minSize = ToolButtonSize;
		b->userData = i;
		b->texture = i->getIcon();
		b->select(i->isActive());
		b->onMouseEnter = [this, i](auto& self)
		{
			self.setText(i->name());
			postSignal({ ui::Signal::Type::NeedsRedraw });
		};
		b->onMouseLeave = [this](auto& self)
		{
			self.setText(L"");
			postSignal({ ui::Signal::Type::NeedsRedraw });
		};
		b->onClick = [this, i](auto& self)
		{
			//self.select();
			i->activate();
			for (auto j : _buttons)
				j->select(((drawing::Tool*)j->userData)->isActive());
			postSignal({ ui::Signal::Type::NeedsRedraw });
		};
	}
}

void DrawingToolbox::processMarkedDelete()
{
	Globals::drawingToolbox = nullptr;
}

void DrawingToolbox::refresh()
{
	for (auto j : _buttons)
		j->select(((drawing::Tool*)j->userData)->isActive());
	postSignal({ ui::Signal::Type::NeedsRedraw });
}

DrawingToolbar::DrawingToolbar(Container* parent) :
	EditorWindow(parent, { 80, 500 }, L"")
{
	assert(Globals::drawingToolbar == nullptr);
	Globals::drawingToolbar = this;
}

void DrawingToolbar::processMarkedDelete()
{
	Globals::drawingToolbar = nullptr;
}